package org.jeecg.modules.AccessoriesCenter.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.AccessoriesCenter.entity.ButtonDaochuEntity;
import org.jeecg.modules.AccessoriesCenter.entity.DilinniDaochuEntity;
import org.jeecg.modules.AccessoriesCenter.entity.DilinniEntity;
import org.jeecg.modules.AccessoriesCenter.service.DilinniManageService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/DilinniManage")
public class DilinniManageController {

    @Autowired
    DilinniManageService service;

    @GetMapping("/DilinniList")
    public Result<?> DilinniList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "status", defaultValue = "10") Integer status,
            String dilinniNumber
    ){
        Page<DilinniEntity> page = new Page<DilinniEntity>(pageNo, pageSize);
        IPage<DilinniEntity> pageList=service.getDilinniList(page,status,dilinniNumber);
        return Result.ok(pageList);
    }

    @GetMapping("/updateDilinnistatus")
    public Result<?> updateDilinnistatus(
            @RequestParam(name = "id") int id,
            @RequestParam(name = "status") int status
    ){
        int count=service.updateDilinnistatus(id,status);
        if(count>0){
            return Result.ok("操作成功");
        }else {
            return Result.error("操作失败");
        }
    }

    @GetMapping("/getDilinnibyId")
    public Result<?> getDilinnibyId(
            @RequestParam(name = "id") int id
    ){
        DilinniEntity entity=service.getDilinnibyId(id);
        return Result.ok(entity);
    }

    @PostMapping("/addDilinni")
    public Result<?> addDilinni(
         @RequestBody DilinniEntity entity
    ){
        //查询一下这个编号是否已经存在
        String cunzai=service.getDilinniNumberbyDilinniNumber(entity.getDilinniNumber());
        if(cunzai!=null){
            return Result.error("该编号已存在,请修改");
        }
        int count=service.addDilinni(entity);
        if(count>0){
            return Result.ok("新增成功");
        }else {
            return Result.error("新增失败");
        }
    }

    @PostMapping("/updateDilinni")
    public Result<?> updateDilinni(
            @RequestBody DilinniEntity entity
    ){
        try {
        int count=service.updateDilinni(entity);
        if(count>0){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("修改失败");
        }
    }


    /**
     * 批量上下架 批量膝盖状态
     */
    @PostMapping("/piliangUpdatestatus")
    public Result<?> piliangUpdatestatus(
            @RequestBody HashMap<String,Object> map

    ){
        try {
            List<Integer> ids=(List<Integer>) map.get("ids");
            int status=(Integer) map.get("status");

            if(ids!=null){
                service.piliangUpdatestatus( ids,status);
            }
            return Result.ok("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }



    /**
     * 导出
     */
    @GetMapping("/downloadDilinni")
    public ModelAndView downloadDilinni(HttpServletRequest request, HttpServletResponse response,
                                        @RequestParam(name = "status", defaultValue = "10") Integer status,
                                        String dilinniNumber
    ) {
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
//	          List<JeecgDemo> pageList = jeecgDemoService.list();
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME,"底领呢列表");
        //注解对象Class
        mv.addObject(NormalExcelConstants.CLASS, DilinniDaochuEntity.class);
        //自定义表格参数
        mv.addObject(NormalExcelConstants.PARAMS,new ExportParams("底领呢列表", "底领呢列表"));
        //导出数据列表
        mv.addObject(NormalExcelConstants.DATA_LIST,data(status,dilinniNumber));
        return mv;
    }

    public List<DilinniDaochuEntity> data(Integer status, String dilinniNumber) {
        List<DilinniDaochuEntity> list = new ArrayList<DilinniDaochuEntity>();
        list=service.getDilinniDaochulist(status,dilinniNumber);
        return list;
    }



    /**
     * 导入纽扣excel
     *
     * @param file  上传的Excel文件
     * @param
     * @return
     */
    @PostMapping("/importExcel")
    public Result<?> importExcel(MultipartFile file) throws Exception {

        ImportParams params = new ImportParams();
        //表格标题所在行，计数从0开始
        params.setTitleRows(1);
        //head头部所在行，计数从0开始
        params.setHeadRows(1);
        //表格sheet数量
        //params.setSheetNum(9);
        // 最好不要设置为true，否则导入一次excel服务器会重启一次，原因不明
        //params.setNeedSave(true);

        InputStream inputStream = file.getInputStream();
        List<DilinniDaochuEntity> list = ExcelImportUtil.importExcel(inputStream, DilinniDaochuEntity.class, params);
        int result = service.importExcel(list);
        if (result > 0) {
            return Result.ok("导入成功");
        } else {
            return Result.ok("导入失败");
        }
    }
}
